import web
import dir
import configure
import re  
import os
urls = (
  "/site=(.*)", "site",
  "/", "index",
  "/mufs/(.*)", "mufs",
  "/tree/(.*)", "tree"
)
global app

dirwebpy = configure.root_dir + r"webpy\\"
def readfile(path):
    print path
    f = file(path)
    data = f.read()
    return data

def html_source(path):
    data = readfile(path)
    reobj = re.compile("<")
    result = reobj.sub("&lt", data)    
    reobj = re.compile(">")
    result = reobj.sub("&gt", result)
    return result


class  tree:
    def GET(self, path):
        self.dir = dirwebpy + r"tree\\"
        return readfile(self.dir + path)        
class  mufs:
    def GET(self, path):
        x = path.split('.')
        print len(x)
        self.dir = configure.root_dir + r"mufs\\"
        print path
        
        if len(x) == 1:
            xx = html_source(self.dir + path)
            print xx
            return "<html>" + xx + "</html>"
        xx = readfile(self.dir + path)
        print xx
        return xx
class  site:
    def GET(self, path):
        print path
    
        return path
class index:
    def GET(self):
        return  dir.run()

def test(path):
    x = path.split('.')
    print len(x)
    dir = configure.root_dir + r"mufs\\"
    print path
    
    if len(x) == 1:
        xx = u"<xmp>" + x + u"</xmp>"
        print xx
        return xx
    print x
    return x


        
def main():
    global app
    app = web.application(urls, globals())
    app.run()

if __name__ == "__main__":
    main()
#    test("41faaac21faaf619653d0314cd0cb55e")
    
